package com.umeox.watch.moto.launcher.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.umeox.watch.moto.launcher.R;
import com.umeox.watch.moto.launcher.bean.TodayChallengeBean;

import java.util.Locale;
import java.util.concurrent.CopyOnWriteArrayList;

public class ChallengeAdapter extends RecyclerView.Adapter<ChallengeAdapter.Holder> {

    public static final int TYPE_WALK = 1;
    public static final int TYPE_RUN = 11;
    public static final int TYPE_SWIM = 12;
    public static final int TYPE_CYCLE = 13;
    public static final int TYPE_FOOTBALL = 21;
    public static final int TYPE_GYM = 22;
    public static final int TYPE_JUMPROPE = 23;
    public static final int TYPE_YOGA = 24;

    public CopyOnWriteArrayList<TodayChallengeBean> getDataList() {
        return mDataList;
    }

    private CopyOnWriteArrayList<TodayChallengeBean> mDataList = new CopyOnWriteArrayList<>();

    private Context mContext;
    private OnItemClickListener mOnItemClickListener;

    private String[] quranNamesArr;


    public ChallengeAdapter(Context context) {
        mContext = context;
        quranNamesArr = mContext.getResources().getStringArray(R.array.quran_name_arr);
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        final View view = LayoutInflater.from(mContext).inflate(R.layout.item_challenge_layout, parent, false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int position) {
        Holder viewHolder = (Holder) holder;
        viewHolder.bind(mDataList.get(position));
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    public void setDataList(CopyOnWriteArrayList<TodayChallengeBean> dataList, int challengeType) {
        if (challengeType == 1) { //运动挑战
            //移除原来集合里为运动挑战的数据
            for (TodayChallengeBean todayChallengeBean : mDataList) {
                if (todayChallengeBean.challengeType == 1) {
                    mDataList.remove(todayChallengeBean);
                }
            }
            mDataList.addAll(dataList);
        } else if (challengeType == 0) {
            //移除原来集合里为古兰经挑战的数据
            for (TodayChallengeBean todayChallengeBean : mDataList) {
                if (todayChallengeBean.challengeType == 0) {
                    mDataList.remove(todayChallengeBean);
                }
            }
            mDataList.addAll(0, dataList); //古兰经数据要放在前面
        }
        notifyDataSetChanged();
    }


    protected class Holder extends RecyclerView.ViewHolder {
        private ImageView mTypeIv;
        private TextView mChallengeLevelTv;
        private TextView mChallengeDescTv;
        private TextView mRewardsTv;
        private com.umeox.moto.watch.themes.widget.ImageView mStartIv;
        private View mItemView;


        public Holder(@NonNull View itemView) {
            super(itemView);
            mTypeIv = itemView.findViewById(R.id.iv_type);
            mChallengeLevelTv = itemView.findViewById(R.id.tv_challenge_level);
            mChallengeDescTv = itemView.findViewById(R.id.tv_challenge_desc);
            mRewardsTv = itemView.findViewById(R.id.tv_rewards);
            mStartIv = itemView.findViewById(R.id.iv_start);
            mItemView = itemView;
        }


        public void bind(TodayChallengeBean todayChallengeBean) {
            int resId = R.drawable.ic_walk;
            String challengeLevelStr = mContext.getResources().getString(R.string.easy_str);
            String challengeDescStr = mContext.getResources().getString(R.string.walk_for_str);
            if (todayChallengeBean.challengeType == 1) {
                switch (todayChallengeBean.sportType) {
                    case TYPE_WALK:
                        resId = R.drawable.ic_walk;
                        challengeLevelStr = mContext.getResources().getString(R.string.easy_str);
                        challengeDescStr = mContext.getResources().getString(R.string.walk_for_str);
                        break;
                    case TYPE_RUN:
                        resId = R.drawable.ic_run;
                        challengeLevelStr = mContext.getResources().getString(R.string.medium_str);
                        challengeDescStr = mContext.getResources().getString(R.string.run_for_str);
                        break;
                    case TYPE_CYCLE:
                        resId = R.drawable.ic_ride;
                        challengeLevelStr = mContext.getResources().getString(R.string.medium_str);
                        challengeDescStr = mContext.getResources().getString(R.string.cycle_for_str);
                        break;
                    case TYPE_FOOTBALL:
                        resId = R.drawable.ic_football;
                        challengeLevelStr = mContext.getResources().getString(R.string.hard_str);
                        challengeDescStr = mContext.getResources().getString(R.string.football_for_str);
                        break;
                    case TYPE_GYM:
                        resId = R.drawable.ic_gym;
                        challengeLevelStr = mContext.getResources().getString(R.string.hard_str);
                        challengeDescStr = mContext.getResources().getString(R.string.gym_for_str);
                        break;
                    case TYPE_JUMPROPE:
                        resId = R.drawable.ic_jump;
                        challengeLevelStr = mContext.getResources().getString(R.string.hard_str);
                        challengeDescStr = mContext.getResources().getString(R.string.jump_rope_for_str);
                        break;
                    case TYPE_YOGA:
                        resId = R.drawable.ic_yoga;
                        challengeLevelStr = mContext.getResources().getString(R.string.hard_str);
                        challengeDescStr = mContext.getResources().getString(R.string.yoga_rope_for_str);
                        break;
                }
                mChallengeDescTv.setText(String.format(Locale.getDefault(), challengeDescStr, todayChallengeBean.duration));

            } else {
                resId = todayChallengeBean.isCompleted ? R.drawable.ic_quran : R.drawable.ic_quran_uncomplete;
                challengeDescStr = mContext.getString(R.string.str_quran_challenge_str);
                mChallengeDescTv.setText(String.format(Locale.getDefault(), challengeDescStr, quranNamesArr[todayChallengeBean.chapterIndex], todayChallengeBean.duration));
            }
            mTypeIv.setImageResource(resId);
            mChallengeLevelTv.setText(challengeLevelStr);
            mRewardsTv.setText(String.format(Locale.getDefault(), "+ %d", todayChallengeBean.rewardValue));

            mStartIv.setOnClickListener(v -> {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(todayChallengeBean);
                }
            });
        }
    }

    public interface OnItemClickListener {
        void onItemClick(TodayChallengeBean challengeBean);
    }


    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }
}
